package com.gm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gm.bean.UserInfo;
import com.gm.mapper.UserMapper;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

/**
 * @Author Administrator
 * @Date 2020/6/23 11:03
 **/

@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //查询
    @GetMapping("/users")
    public String findAll(Model model, @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "5") Integer size){
        PageHelper.startPage(page,size);
        List<UserInfo> userList = this.userMapper.findAll(page,size);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userList);
        model.addAttribute("users",pageInfo);
        return "user-list";
    }
    //增加
    @PostMapping("/user")
    public String addUser(UserInfo userInfo){
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        this.userMapper.addUser(userInfo);
        return "redirect:/users";
    }
    //删除
    @DeleteMapping("/user/{id}")
    public String delUser(@PathVariable("id") Integer id){
        this.userMapper.delUser(id);
        return "redirect:/../users";
    }
}
